package com.athengzhi.speingCloud.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @Package: com.athengzhi.speingCloud.filter
 * @ClassName: GlobalFilter
 * @Author: lhz
 * @CreateTime: 2023/5/22 14:29
 * @Description:
 */
@Component
@Slf4j
class MyGlobalFilter implements GlobalFilter, Ordered {

    @Override
    public int getOrder() {
        return 0;
    }

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.info("come in MyGlobalFilter---");
        String username = exchange.getRequest().getQueryParams().getFirst("username");//获取请求参数里面的username
        if(username == null ){
            log.info("username为null，参数违法o(╥﹏╥)o");
            exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
            return exchange.getResponse().setComplete();
        }
        return chain.filter(exchange);
    }
}
